from classes.TestStep import TestStep
from classes.TestCase import TestCase
from classes.SCMAutoTestManager import SCMAutoTestManager
from classes.DataImporter import DataImporter

  
# 3. main函数调用SCMAutoTestManager的testXXXX方法
def run_test():
    api_url = "http://127.0.0.1:5000/api/test/execute"

    manager = SCMAutoTestManager(api_url=api_url)

    # 构造输入数据
    input_data = {
        "username": "admin",
        "password": "123456"
    }
    
    input_cust_data = {            
        "customer_code": "KH001",
        "customer_short_name": "测试客户简称",
        "customer_full_name": "测试客户全称",
        "owner": "张三",
        "phone": "13800000000",
        "address": "北京市海淀区XX路",
        "email": "test@example.com",
        "country": "中国",
        "business_owner": "李四",
        "credit_code": "1234567890ABCDEF",
        "bank": "中国银行",
        "bank_account": "6222021234567890",
        "customer_remark": "这是一个测试备注"
    }

    # 客户信息  
    col_mapping_to_customer_basicInfo = {
        "客户NO.": "customer_code",
        "备注": "customer_short_name",
        "客户名称": "customer_full_name",
        "姓名": "owner",
        "电话": "phone",
        "公司地址": "address",
        "邮箱": "email",
        "区分": "country",
        "RD现对应者": "business_owner",
        "信用代码": "credit_code",
        "开户行": "bank",
        "银行账号": "bank_account",
        "备注": "customer_remark"
    }

    # 供应商信息
    col_mapping_to_supplier_basicInfo = {
        "供应商NO.": "supplier_basicInfo_code",
        "没有对应列1": "supplier_basicInfo_short_name",
        "供应商名称": "supplier_basicInfo_full_name",
        "信息": "supplier_basicInfo_owner",
        "电话": "supplier_basicInfo_phone",
        "公司地址": "supplier_basicInfo_address",
        "邮箱": "supplier_basicInfo_email",
        "区分": "supplier_basicInfo_domesticOrCrossboard",
        "没有对应列2": "supplier_basicInfo_country",
        "RD现对应者": "supplier_basicInfo_business_owner",
        "没有对应列3": "supplier_basicInfo_credit_code",
        "没有对应列4": "supplier_basicInfo_bank",
        "没有对应列5": "supplier_basicInfo_bank_account",
        "备注": "supplier_basicInfo_remark"
    }

    importer = DataImporter("./custData/custBasicInfoData.xlsx",  col_mapping_to_supplier_basicInfo, sheet_name="供应商信息")

    test_id = "my_test_id_A_101"
    result = manager.test_login(input_data=input_data, test_id=test_id)
    result = manager.test_navigate_supplier_basic_info(test_id=test_id)

    for test_data in importer.iter_test_data():
        # 如果“供应商名称”为空，则停止处理
        if not test_data.get("supplier_basicInfo_full_name") or str(test_data.get("supplier_basicInfo_full_name")).strip() == "":
            print("检测到供应商名称为空，停止导入。")
            break
        # 保证所有字段都不为None，空值用空字符串
        for key in [
            "supplier_basicInfo_code",
            "supplier_basicInfo_short_name",
            "supplier_basicInfo_full_name",
            "supplier_basicInfo_owner",
            "supplier_basicInfo_phone",
            "supplier_basicInfo_address",
            "supplier_basicInfo_email",
            "supplier_basicInfo_domesticOrCrossboard",
            "supplier_basicInfo_country",
            "supplier_basicInfo_business_owner",
            "supplier_basicInfo_credit_code",
            "supplier_basicInfo_bank",
            "supplier_basicInfo_bank_account",
            "supplier_basicInfo_remark"
        ]:
            if key not in test_data or test_data[key] is None:
                test_data[key] = ""
        print(test_data)  # 可直接用于测试用例
        result = manager.test_add_supplier_basic_info(input_data=test_data,test_id=test_id)



if __name__ == "__main__":
    run_test()